package com.community.community.controller;

import com.community.community.pojo.Result;
import com.community.community.pojo.dto.GoodDto;
import com.community.community.pojo.entity.Good;
import com.community.community.pojo.vo.GoodVo;
import com.community.community.pojo.vo.UserVo;
import com.community.community.service.GoodService;
import com.community.community.untils.OssUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/good")
public class GoodController {

    @Autowired
    private OssUtil ossUtil;

    @Autowired
    private GoodService goodService;

    /**
     * 上传图片到 OSS
     * @param file 上传的文件
     * @return 文件在 OSS 中的 URL
     */
    @PostMapping("/uploadImage")
    public Result uploadImage(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            throw new RuntimeException("文件不能为空");
        }
        String url = ossUtil.uploadFile(file);
        return Result.success((Object) url);
    }

    @PostMapping("/add")
    public Result addGood(@Valid @RequestBody GoodDto goodDto,
                          HttpServletRequest request){
        return goodService.add(request,goodDto);

    }

    @GetMapping("/commandlist")
    public Result<List<GoodVo>> commandList(@Param("page") Integer page ){
        return goodService.commandList(page);
    }

    @GetMapping("/detail")
    public Result<Good> detail(@RequestParam("id") Integer id){
        return goodService.detail(id);
    }

}